package com.crimsec.utils.snmp.model.dao;

import com.crimsec.utils.snmp.model.IConstants;
import com.crimsec.utils.snmp.model.Mibent;
import javax.persistence.EntityManager;
import javax.persistence.NoResultException;
import javax.persistence.PersistenceException;

/**
 *
 * @author moroz
 */
public class MibentDAO extends AbstractDAO {

    public static Mibent findMibentByName(String name, EntityManager em) throws PersistenceException {
        Mibent result;

        if ((name != null) && (!name.isEmpty())) {
            try {
                result = em.createNamedQuery("Mibent.findByName", Mibent.class)
                        .setParameter("name", name).getSingleResult();
            } catch (NoResultException ex) {//
                result = em.createNamedQuery("Mibent.findByName", Mibent.class)
                        .setParameter("name", IConstants.unknown)
                        .getSingleResult();
            }
            return result;
        } else {
            throw new PersistenceException("given mibent name is empty or null");
        }
    }
}
